/**
 * 
 */
package com.infore.statisticsAndAlarm.util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


/**
 * @author yuxing
 *
 */
public class GetAirQualityComIndex {
	//各个因子浓度二级限值(年平均)
	private final static int SO2_LIMITS_2 = 60;
	private final static int NO2_LIMITS_2 = 40;
	private final static int CO_LIMITS_2 = 4;
	private final static int O3_LIMITS_2 = 160;
	private final static int PM10_LIMITS_2 = 70;
	private final static int PM25_LIMITS_2 = 35;
	
	//各个因子浓度二级限值(24小时平均)
	private final static int SO2_LIMITS24H_2 = 150;
	private final static int NO2_LIMITS24H_2 = 80;
	private final static int CO_LIMITS24H_2 = 4;
	private final static int O3_LIMITS24H_2 = 160;
	private final static int PM10_LIMITS24H_2 = 150;
	private final static int PM25_LIMITS24H_2 = 75;
	
	//各个因子浓度二级限值(1小时平均)
	private final static int SO2_LIMITS1H_2 = 500;
	private final static int NO2_LIMITS1H_2 = 200;
	private final static int CO_LIMITS1H_2 = 10;
	private final static int O3_LIMITS1H_2 = 200;
	private final static int PM10_LIMITS1H_2 = 150;
	private final static int PM25_LIMITS1H_2 = 75;
	
	public static void main(String[] args) {
		Map<String, Double> map = new  HashMap<String, Double>();
		map.put("PM25", 32.0);
		map.put("PM10", 56.0);
		map.put("SO2", 6.0);
		map.put("NO2", 28.0);
		map.put("CO", 1.0);
		map.put("O3", 50.0);
		System.out.println(getAQCI(map));
		System.out.println(getO3_subIndex(78.0));
	}
	
	

	//各单项因子单个指数计算方法(年平均)
	public static double getSO2_subIndex(double SO2){
		double SO2_subIndex = new BigDecimal(SO2/SO2_LIMITS_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return SO2_subIndex;
	}
	
	public static double getNO2_subIndex(double NO2){
		double NO2_subIndex = new BigDecimal(NO2/NO2_LIMITS_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return NO2_subIndex;
	}
	
	public static double getCO_subIndex(double CO){
		double CO_subIndex = new BigDecimal(CO/CO_LIMITS_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return CO_subIndex;
	}
	
	public static double getO3_subIndex(double O3){
		double O3_subIndex = new BigDecimal(O3/O3_LIMITS_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return O3_subIndex;
	}
	
	public static double getPM10_subIndex(double PM10){
		double PM10_subIndex = new BigDecimal(PM10/PM10_LIMITS_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return PM10_subIndex;
	}
	
	public static double getPM25_subIndex(double PM25){
		double PM25_subIndex = new BigDecimal(PM25/PM25_LIMITS_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return PM25_subIndex;
	}
	
	//各单项因子单个指数计算方法(24小时平均)
	public static double getSO2_subIndex24H(double SO2){
		double SO2_subIndex = new BigDecimal(SO2/SO2_LIMITS24H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return SO2_subIndex;
	}
	
	public static double getNO2_subIndex24H(double NO2){
		double NO2_subIndex = new BigDecimal(NO2/NO2_LIMITS24H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return NO2_subIndex;
	}
	
	public static double getCO_subIndex24H(double CO){
		double CO_subIndex = new BigDecimal(CO/CO_LIMITS24H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return CO_subIndex;
	}
	
	public static double getO3_subIndex24H(double O3){
		double O3_subIndex = new BigDecimal(O3/O3_LIMITS24H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return O3_subIndex;
	}
	
	public static double getPM10_subIndex24H(double PM10){
		double PM10_subIndex = new BigDecimal(PM10/PM10_LIMITS24H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return PM10_subIndex;
	}
	
	public static double getPM25_subIndex24H(double PM25){
		double PM25_subIndex = new BigDecimal(PM25/PM25_LIMITS24H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return PM25_subIndex;
	}
	
	//各单项因子单个指数计算方法(1小时平均)
	public static double getSO2_subIndex1H(double SO2){
		double SO2_subIndex = new BigDecimal(SO2/SO2_LIMITS1H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return SO2_subIndex;
	}
	
	public static double getNO2_subIndex1H(double NO2){
		double NO2_subIndex = new BigDecimal(NO2/NO2_LIMITS1H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return NO2_subIndex;
	}
	
	public static double getCO_subIndex1H(double CO){
		double CO_subIndex = new BigDecimal(CO/CO_LIMITS1H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return CO_subIndex;
	}
	
	public static double getO3_subIndex1H(double O3){
		double O3_subIndex = new BigDecimal(O3/O3_LIMITS1H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return O3_subIndex;
	}
	
	public static double getPM10_subIndex1H(double PM10){
		double PM10_subIndex = new BigDecimal(PM10/PM10_LIMITS1H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return PM10_subIndex;
	}
	
	public static double getPM25_subIndex1H(double PM25){
		double PM25_subIndex = new BigDecimal(PM25/PM25_LIMITS1H_2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return PM25_subIndex;
	}
	
	//环境质量综合指数(年平均)
	public static double getAQCI(Map<String, Double> map){
		double AQCI = 0;
		double ISO2 = 0;
		double INO2 = 0;
		double ICO = 0;
		double IO3 = 0;
		double IPM10 = 0;
		double IPM25 = 0;
		if(map.get("SO2") != null){
			ISO2 = getSO2_subIndex(map.get("SO2"));
		}
		if(map.get("NO2") != null){
			INO2 = getNO2_subIndex(map.get("NO2"));
		}
		if(map.get("CO") != null){
			ICO = getCO_subIndex(map.get("CO"));
		}
		if(map.get("O3") != null){
			IO3 = getO3_subIndex(map.get("O3"));
		}
		if(map.get("PM10") != null){
			IPM10 = getPM10_subIndex(map.get("PM10"));
		}
		if(map.get("PM25") != null){
			IPM25 = getPM25_subIndex(map.get("PM25"));
		}
		AQCI = new BigDecimal(ISO2+INO2+ICO+IO3+IPM10+IPM25).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return AQCI;
	}
	
	//国标----环境质量综合指数(年平均)
	public static double getGAQCI(Map<String, Double> map, Map<String, Double> gmap){
		double AQCI = 0;
		double ISO2 = 0,GSO2 = 0,IGSO2 = 0;
		double INO2 = 0,GNO2 = 0,IGNO2 = 0;
		double ICO = 0,GCO = 0,IGCO = 0;
		double IO3 = 0,GO3 = 0,IGO3 = 0;
		double IPM10 = 0,GPM10 = 0,IGPM10 = 0;
		double IPM25 = 0,GPM25 = 0,IGPM25 = 0;
		if(map.get("SO2") != null && gmap.get("PER_SO2") != null){
			ISO2 = getSO2_subIndex(map.get("SO2"));
			GSO2 = getSO2_subIndex24H(gmap.get("PER_SO2"));
			IGSO2 = gMax(ISO2,GSO2);
		}
		if(map.get("NO2") != null && gmap.get("PER_NO2") != null){
			INO2 = getNO2_subIndex(map.get("NO2"));
			GNO2 = getNO2_subIndex24H(gmap.get("PER_NO2"));
			IGNO2 = gMax(INO2,GNO2);
		}
		if(map.get("CO") != null && gmap.get("PER_CO") != null){
			ICO = getCO_subIndex(map.get("CO"));
			GCO = getCO_subIndex24H(gmap.get("PER_CO"));
			IGCO = gMax(ICO,GCO);
		}
		if(map.get("O3") != null && gmap.get("PER_O3") != null){
			IO3 = getO3_subIndex(map.get("O3"));
			GO3 = getO3_subIndex24H(gmap.get("PER_O3"));
			IGO3 = gMax(IO3,GO3);
		}
		if(map.get("PM10") != null && gmap.get("PER_PM10") != null){
			IPM10 = getPM10_subIndex(map.get("PM10"));
			GPM10 = getPM10_subIndex24H(gmap.get("PER_PM10"));
			IGPM10 = gMax(IPM10,GPM10);
		}
		if(map.get("PM25") != null && gmap.get("PER_PM25") != null){
			IPM25 = getPM25_subIndex(map.get("PM25"));
			GPM25 = getPM25_subIndex24H(gmap.get("PER_PM25"));
			IGPM25 = gMax(IPM25,GPM25);
		}
		AQCI = new BigDecimal(IGSO2+IGNO2+IGCO+IGO3+IGPM10+IGPM25).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return AQCI;
	}
	
	//环境质量综合指数(24小时平均)
	public static double getAQCI24H(Map<String, Double> map){
		double AQCI = 0;
		double ISO2 = 0;
		double INO2 = 0;
		double ICO = 0;
		double IO3 = 0;
		double IPM10 = 0;
		double IPM25 = 0;
		if(map.get("SO2") != null){
			ISO2 = getSO2_subIndex24H(map.get("SO2"));
		}
		if(map.get("NO2") != null){
			INO2 = getNO2_subIndex24H(map.get("NO2"));
		}
		if(map.get("CO") != null){
			ICO = getCO_subIndex24H(map.get("CO"));
		}
		if(map.get("O3") != null){
			IO3 = getO3_subIndex24H(map.get("O3"));
		}
		if(map.get("PM10") != null){
			IPM10 = getPM10_subIndex24H(map.get("PM10"));
		}
		if(map.get("PM25") != null){
			IPM25 = getPM25_subIndex24H(map.get("PM25"));
		}
		AQCI = new BigDecimal(ISO2+INO2+ICO+IO3+IPM10+IPM25).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return AQCI;
	}
	
	//环境质量综合指数(1小时平均)
	public static double getAQCI1H(Map<String, Double> map){
		double AQCI = 0;
		double ISO2 = 0;
		double INO2 = 0;
		double ICO = 0;
		double IO3 = 0;
		double IPM10 = 0;
		double IPM25 = 0;
		if(map.get("SO2") != null){
			ISO2 = getSO2_subIndex1H(map.get("SO2"));
		}
		if(map.get("NO2") != null){
			INO2 = getNO2_subIndex1H(map.get("NO2"));
		}
		if(map.get("CO") != null){
			ICO = getCO_subIndex1H(map.get("CO"));
		}
		if(map.get("O3") != null){
			IO3 = getO3_subIndex1H(map.get("O3"));
		}
		if(map.get("PM10") != null){
			IPM10 = getPM10_subIndex1H(map.get("PM10"));
		}
		if(map.get("PM25") != null){
			IPM25 = getPM25_subIndex1H(map.get("PM25"));
		}
		AQCI = new BigDecimal(ISO2+INO2+ICO+IO3+IPM10+IPM25).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return AQCI;
	}
	
	//最大质量指数(年平均)
	public static double getSubIndex(Map<String, Double> map){
		double IAQI_SO2_1H =0;
		double IAQI_NO2_1H =0;
		double IAQI_PM10_1H =0;
		double IAQI_CO_1H =0;
		double IAQI_O3_1H =0;
		double IAQI_PM25_1H =0;
		double IAQI_O3_8H = 0;
		if(map.get("SO2") !=null){
			 IAQI_SO2_1H = getSO2_subIndex(map.get("SO2"));
		}
		if(map.get("NO2") !=null){
			IAQI_NO2_1H = getNO2_subIndex(map.get("NO2"));
		}
		if(map.get("PM10") !=null){
			 IAQI_PM10_1H = getPM10_subIndex(map.get("PM10"));
		}
		if(map.get("CO") !=null){
			 IAQI_CO_1H = getCO_subIndex(map.get("CO"));
		}
		if(map.get("O3") !=null){
			 IAQI_O3_1H = getO3_subIndex(map.get("O3"));
		}
		if(map.get("PM25") !=null){
			 IAQI_PM25_1H = getPM25_subIndex(map.get("PM25"));
		}
		if(map.get("O38H") != null){
			IAQI_O3_8H = getO3_subIndex(map.get("O38H"));
		}
		
		double IAQCI = Max(IAQI_SO2_1H, IAQI_NO2_1H, IAQI_PM10_1H, IAQI_CO_1H, IAQI_O3_1H, IAQI_PM25_1H,IAQI_O3_8H);
		return IAQCI;
	}
	
	
	//最大质量指数(24小时平均)
	public static double getSubIndex24H(Map<String, Double> map){
		double IAQI_SO2_1H =0;
		double IAQI_NO2_1H =0;
		double IAQI_PM10_1H =0;
		double IAQI_CO_1H =0;
		double IAQI_O3_1H =0;
		double IAQI_PM25_1H =0;
		double IAQI_O3_8H = 0;
		if(map.get("SO2") !=null){
			 IAQI_SO2_1H = getSO2_subIndex24H(map.get("SO2"));
		}
		if(map.get("NO2") !=null){
			IAQI_NO2_1H = getNO2_subIndex24H(map.get("NO2"));
		}
		if(map.get("PM10") !=null){
			 IAQI_PM10_1H = getPM10_subIndex24H(map.get("PM10"));
		}
		if(map.get("CO") !=null){
			 IAQI_CO_1H = getCO_subIndex24H(map.get("CO"));
		}
		if(map.get("O3") !=null){
			 IAQI_O3_1H = getO3_subIndex24H(map.get("O3"));
		}
		if(map.get("PM25") !=null){
			 IAQI_PM25_1H = getPM25_subIndex24H(map.get("PM25"));
		}
		if(map.get("O38H") != null){
			IAQI_O3_8H = getO3_subIndex24H(map.get("O38H"));
		}
		
		double IAQCI = Max(IAQI_SO2_1H, IAQI_NO2_1H, IAQI_PM10_1H, IAQI_CO_1H, IAQI_O3_1H, IAQI_PM25_1H,IAQI_O3_8H);
		return IAQCI;
	}
	
	//最大质量指数(年平均)
	public static double getSubIndex1H(Map<String, Double> map){
		double IAQI_SO2_1H =0;
		double IAQI_NO2_1H =0;
		double IAQI_PM10_1H =0;
		double IAQI_CO_1H =0;
		double IAQI_O3_1H =0;
		double IAQI_PM25_1H =0;
		double IAQI_O3_8H = 0;
		if(map.get("SO2") !=null){
			 IAQI_SO2_1H = getSO2_subIndex1H(map.get("SO2"));
		}
		if(map.get("NO2") !=null){
			IAQI_NO2_1H = getNO2_subIndex1H(map.get("NO2"));
		}
		if(map.get("PM10") !=null){
			 IAQI_PM10_1H = getPM10_subIndex1H(map.get("PM10"));
		}
		if(map.get("CO") !=null){
			 IAQI_CO_1H = getCO_subIndex1H(map.get("CO"));
		}
		if(map.get("O3") !=null){
			 IAQI_O3_1H = getO3_subIndex1H(map.get("O3"));
		}
		if(map.get("PM25") !=null){
			 IAQI_PM25_1H = getPM25_subIndex1H(map.get("PM25"));
		}
		if(map.get("O38H") != null){
			IAQI_O3_8H = getO3_subIndex1H(map.get("O38H"));
		}
		
		double IAQCI = Max(IAQI_SO2_1H, IAQI_NO2_1H, IAQI_PM10_1H, IAQI_CO_1H, IAQI_O3_1H, IAQI_PM25_1H,IAQI_O3_8H);
		return IAQCI;
	}
	
	
	//计算单项指数、综合指数所对应的级别
	public static String getIndexLevel(double index){
		String level = "";
		if(index > 0 && index <= 1.3){
			level = "一级";
		}else if(index > 1.3 && index <= 4){
			level = "二级";
		}else if(index > 4 && index <= 8){
			level = "三级";
		}else if(index > 8 && index <= 12){
			level = "四级";
		}else if(index > 12){
			level = "五级";
		}
		return level;
	}
	
	
	public static double Max(double ISO2, double INO2, double IPM10 , double ICO, double IO3, double IPM25,double IO38H){
	 	double	max = 0;
	 	if(max < ISO2){
	 		max = ISO2;
	 	}
	 	if(max < INO2){
	 		max = INO2;
	 	}
	 	if(max < IPM10){
	 		max=IPM10;
	 	}
	 	if(max < ICO){
	 		max = ICO;
	 	}
	 	if(max < IO3){
	 		max =IO3;
	 	}
	 	if(max < IPM25){
	 		max=IPM25;
	 	}
	 	if(max < IO38H){
	 		max = IO38H;
	 	}
	 	return max;
	}
	
	public static double gMax(double IA, double IB){
	 	double	max = 0;
	 	if(max < IA){
	 		max = IA;
	 	}
	 	if(max < IB){
	 		max = IB;
	 	}
	 	return max;
	}
	
}
